<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">WallPaper后台管理系统</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
        </el-input>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:67%;"
          class="login-button"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <div class="li-block">
      <div class="square">
        <ul>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>
          <li></li>

        </ul>
      </div>
      <div class="circle">
        <ul>
          <!-- 这个是横向 循环位移 -->
          <li></li>
          <li></li>
        </ul>
      </div>
    </div>

    <div class="el-login-footer">
      <span>icp备案:   <a href="https://beian.miit.gov.cn/">豫ICP备2024052256号-1</a></span>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import Cookies from 'js-cookie'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        username: 'yuhao',
        password: 'yuhao123',
        rememberMe: false
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
      },
      loading: false,
      // 验证码开关
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    handleLogin() {
      //使用el-form的API对表单进行校验
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', this.loginForm.password, { expires: 30 })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
          }
          console.log('登录请求....')
          this.$store.dispatch('user/login', this.loginForm)
            .then((res) => {
              this.$router.push('/dashboard')
            })
            .catch((err) => {
              Message.error(err)
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background-image: linear-gradient(to top, #a8edea 0%, #fed6e3 100%);
  background-size: cover;
}

.title {
  margin: 0px auto 30px auto;
  padding: 10px;
  text-align: center;
  color: #707070;
}

.login-button {
  height: 35px;
  width: 65%;
  /*文本间隔*/
  letter-spacing: 7px;
  //margin-left: 30px;
  color: beige;
  background: linear-gradient(45deg, #e6dde0, #8985e6);
  border-radius: 20px;
  cursor: pointer;
}


.login-button:hover {
  background: linear-gradient(45deg, #8985e6, #e6dde0);
  cursor: pointer;
}

.login-form {
  border-radius: 13px;
  background: #ffffff;
  text-align: center;
  width: 350px;
  z-index: 4;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: gray;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>

<style lang="css">
.square li, .circle li {
  position: absolute;
  left: 0;
  top: 0;
  width: 3.5vh;
  height: 3.5vh;
  list-style: none;
  z-index: 3;
  border: 1px papayawhip solid;
  box-shadow: 0 0 10px #bcb4e4;
}

.square li {
  animation: squares 4s linear infinite;
  border-radius: 15%;
}

.square li:nth-child(1) {
  left: 15vw;
  top: 15vh;
  animation-delay: 0.1s;
}

.square li:nth-child(2) {
  left: 45vw;
  top: 15vh;
  animation-delay: 0.2s;
}

.square li:nth-child(3) {
  left: 80vw;
  top: 15vh;
}

.square li:nth-child(4) {
  left: 25vw;
  top: 81vh;
  animation-delay: 0.4s;
}

.square li:nth-child(5) {
  left: 67vw;
  top: 81vh;
  animation-delay: 0.7s;
}

/**
   * circle的相对位置
 */

.circle li {
  animation: circle 7s linear infinite;
  bottom: 10px;
  border-radius: 15%;
  z-index: 3;
  opacity: 0.1;
}

.circle li:nth-child(1) {
  top: 50%;
  width: 2.5vh;
  height: 2.5vh;
  animation: left-li 6s linear infinite;
}

.circle li:nth-child(2) {
  top: 60%;
  width: 2.5vh;
  height: 2.5vh;
  animation: right-li 6s linear infinite;
}


@keyframes squares {
  0% {
    transform: scale(1) rotateY(0deg);
  }

  100% {
    transform: scale(2.5) rotateY(800deg);
    opacity: 0.2;
  }
}


@keyframes circle {
  0% {
    transform: scale(1) rotateY(0deg);
    bottom: 0;
    border-radius: 10%;
  }

  100% {
    transform: scale(3) rotateY(800deg);
    border-radius: 30%;
    bottom: 90vh;
    opacity: 0.5;
  }
}


@keyframes left-li {
  0% {
    transform: scale(1) rotateY(0deg);
    opacity: 1;
    left: 0;
  }

  100% {
    transform: scale(2.5) rotateY(800deg);
    opacity: 0.5;
    left: 80vw;
  }
}

@keyframes right-li {
  0% {
    transform: scale(1) rotateY(0deg);
    left: 90vw;
  }

  100% {
    transform: scale(2.5) rotateY(800deg);
    opacity: 1;
    right: 80vw;
  }
}
</style>
